package com.uppfind.framework.core.service.impl;

import com.uppfind.framework.core.bean.GenericPo;
import com.uppfind.framework.core.dao.api.*;
import com.uppfind.framework.core.exceptions.NotFoundException;
import com.uppfind.framework.core.service.GenericService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 抽象通用服务实现类，通过指定{@link GenericMapper} 实现通用的增删改查方法
 * @param <PO> PO类型
 * @param <PK> 主键类型
 *             Created by xuliugen on 2017/6/29.
 * @see GenericService
 * @since 1.0
 */
public abstract class AbstractServiceImpl<PO extends GenericPo<PK>, PK> implements GenericService<PO, PK>
        , SimpleQueryService<PO, PK>
        , SimpleUpdateService<PO, PK>
        , SimpleDeleteService<PK>
        , SimpleInsertService<PO, PK> {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    protected abstract GenericMapper<PO, PK> getMapper();

    @Override
    public InsertMapper getInsertMapper() {
        return getMapper();
    }

    @Override
    public QueryMapper<PO, PK> getQueryMapper() {
        return getMapper();
    }

    @Override
    public UpdateMapper<PO> getUpdateMapper() {
        return getMapper();
    }

    @Override
    public DeleteMapper getDeleteMapper() {
        return getMapper();
    }

    protected void assertNotNull(Object po, String message) {
        if (po == null) {
            throw new NotFoundException(message);
        }
    }

    protected void assertNotNull(Object po) {
        assertNotNull(po, "数据不存在");
    }

}
